//
//  RegisterUserScreen.m
//  TowerDefence
//
//  Created by k3 on 6/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterUserScreen.h"
#import "iCoreGUIController.h"
#import "GameOverPopUp.h"

#define IMAGE_NAME_BG_TEXTFIELD @"Images/Popup/popupHeaderBackground.png"
#define IMAGE_FLOWER @"Images/PlayScreen/hoa.jpeg"

@implementation RegisterUserScreen

-(id)initRegisterUserScreenWith:(iCoreGUIController *)_mainCore {
    self = [super initGameLayerWith:_mainCore];
    mainGUIController = _mainCore;
    [self loadResource];
    return self;
}

-(void)loadResource {
    
    blankImg = [CCSprite spriteWithFile:IMAGE_FLOWER];
    [blankImg setPosition:ccp(0, 0)];
    [blankImg setAnchorPoint:ccp(0, 0)];
    [blankImg setScale:0.9];
    [self addChild:blankImg z:0];

    background = [[CCSprite alloc] initWithFile:IMAGE_BLANK_BROWN];
    size = [[CCDirector sharedDirector] winSize];
    [background setPosition:ccp(size.width/2,size.height/2)];
    [self addChild:background z:1];
    
    txtUserName = [[UITextField alloc] initWithFrame:CGRectMake(160, 80, 180, 30)];
    txtUserName.textColor = [UIColor yellowColor];
    txtUserName.returnKeyType = UIReturnKeyDone;
    txtUserName.placeholder = @"Enter Your Name";
    txtUserName.font = [UIFont fontWithName:FONT_MARKER_FELT size:25];
    txtUserName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    txtUserName.delegate = self;
    [txtUserName becomeFirstResponder];
    //[[UIApplication sharedApplication] delegate];
    [[[CCDirector sharedDirector] openGLView] addSubview:txtUserName];
    //[background addChild:view];
    //[view addSubview:txtUserName];
    
    CCSprite *btnSave = [CCSprite spriteWithFile:IMAGE_MAIN_MENU_BUTTON_NORMAL];
    [btnSave setPosition:ccp(background.contentSize.width/2-50, background.contentSize.height/2-50)];
    [background addChild:btnSave z:1];
    
    CCLabelTTF *lblSave = [CCLabelTTF labelWithString:@"Save" fontName:FONT_MARKER_FELT fontSize:28];
    [lblSave setColor:ccc3(255, 255, 64)];
    CCMenuItemLabel *saveMenuItemLabel = [CCMenuItemLabel itemWithLabel:lblSave target:self selector:@selector(saveUserData)];
    CCMenu *saveMenu = [CCMenu menuWithItems:saveMenuItemLabel, nil];
    [saveMenu setPosition:ccp(btnSave.contentSize.width/2, btnSave.contentSize.height/2)];
    [btnSave addChild:saveMenu z:1];
    
    CCSprite *btnBack = [CCSprite spriteWithFile:IMAGE_MAIN_MENU_BUTTON_NORMAL];
    [btnBack setPosition:ccp(background.contentSize.width/2+50, background.contentSize.height/2-50)];
    [background addChild:btnBack z:1];
    
    CCLabelTTF *lblBack = [CCLabelTTF labelWithString:@"Back" fontName:FONT_MARKER_FELT fontSize:28];
    [lblBack setColor:ccc3(255, 255, 64)];
    CCMenuItemLabel *backMenuItemLabel = [CCMenuItemLabel itemWithLabel:lblBack target:self selector:@selector(testToGameOverPopUp)];
    CCMenu *backMenu = [CCMenu menuWithItems:backMenuItemLabel, nil];
    [backMenu setPosition:ccp(btnBack.contentSize.width/2, btnBack.contentSize.height/2)];
    [btnBack addChild:backMenu z:1];
}


-(void)saveUserData {
    if(txtUserName.text.length == 0) {
        NSLog(@"User cannot be Blank");
    }
    else {
        NSLog(@"Save userName: %@",txtUserName.text);
        //[self writeToTextFile:txtUserName.text];
        //userName = txtUserName.text;
        [self goBackToMainScreen];
    }
}

-(void)testToGameOverPopUp {
    [txtUserName removeFromSuperview];
    [txtUserName release];
    GameOverPopUp *testPopUpGameOver = [[GameOverPopUp alloc] initGameOverPopUpWithWith:self];
    [testPopUpGameOver loadGameOverScreen];
}

-(void)goBackToMainScreen {
    //userName = txtUserName.text;
    [txtUserName removeFromSuperview];
    [mainGUIController PlayMainMenuScreenWithCurrentScreen:self];
}

-(void)writeToTextFile:(NSString *)_text {
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"%@",documentsDirectory);
    
    //NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UserData" ofType:@"txt"];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"/UserData.txt"];
    NSString *content = [_text stringByAppendingString:@"/n"]; 
    // NSData *data = [[NSData alloc] initWithContentsOfFile:content];
    [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"===%@",txtUserName.text);
    mainGUIController.playerData.playerName = txtUserName.text;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [txtUserName resignFirstResponder];
    return YES;
}

@end
